#ifndef     __SDL_AUDIO_H
#define     __SDL_AUDIO_H

#include "../utils/audio_param.h"
#include "../../utils/nocopyable.h"
#include "../utils/audio_buf.h"

class SDLAudio : private utils::NoCopyable {

public:
    SDLAudio(const AudioParam &param) {
        SDL_AudioSpec spec, temp;
        spec.freq = param.sample_rate;
        spec.format = param.sample_fmt;
        spec.channels = param.sample_chan;
        spec.samples = 1024;
        spec.silence = 0;
        spec.callback = nullptr;
        spec.userdata = nullptr;

        m_id = SDL_OpenAudioDevice(
            nullptr, 0, &spec, &temp, 0
        );
        if (m_id <= 0) {
            throw SDLException("SDL_OpenAudioDevice");
        }
        SDL_PauseAudioDevice(m_id, 0);
    }

    ~SDLAudio() {
        SDL_CloseAudioDevice(m_id);
    }

    void write(const AudioBuf &buf) {
        SDL_QueueAudio(m_id, buf.buf, buf.len);
    }

private:
    SDL_AudioDeviceID m_id;
};

#endif
